import 'package:just_audio/just_audio.dart';

class SoundMan {
  static final SoundMan instance = SoundMan._interval();
  factory SoundMan() => instance;
  SoundMan._interval() {
    _player = AudioPlayer();
  }

  late AudioPlayer _player;
  Stream<bool> get playingStream => _player.playingStream;

  Future<void> play(String? path) async {
    if (path == null || path == '') {
      return;
    }
    if (_player.playing) {
      await stop();
    }
    AudioSource source;
    if (path.startsWith('http')) {
      source = AudioSource.uri(Uri.parse(path));
    } else {
      source = AudioSource.file(path);
    }
    await _player.setAudioSource(source);
    await _player.play();
  }

  Future<void> stop() async {
    if (_player.playing) {
      await _player.stop();
    }
  }
}
